package com.huaonline.music.player;

import java.util.ArrayList;
import java.util.Map;

import com.huaonline.music.player.adapter.MusicListAdapter;
import com.huaonline.music.player.commonutils.FileCommonUtil;
import com.huaonline.music.player.commonutils.ToastCommonUtils;
import com.huaonline.music.player.server.IMusicControlServer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class HuaPlayerActivity extends BaseAdapter implements OnClickListener {

	private Context ctx;
	private Button tempButton;
	private Button play_main_btn;
	private Button prevail_main_btn;
	private Button next_main_btn;

	private MusicControlServerConnect controlServerConnect;
	private IMusicControlServer musicControlServer;
	private ListView mianplayer_music_viewlist;
	private MusicListAdapter mianplayer_music_adapter;
	private ArrayList<Map<String, String>> music_data;
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initMusicServer();
		setContentView(R.layout.huaplayer_main);
		this.ctx = this;
		initView();
		temp_getdata();
		addMusicList();

	}

	private void initView() {
		// init views
		mianplayer_music_viewlist = (ListView) findViewById(R.id.mianplayer_music_viewlist);
		mianplayer_music_viewlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				ToastCommonUtils.getToastShort("TTTT", ctx);
				try {
					musicControlServer.setItem(music_data.get(arg2).get(FileCommonUtil.AUDIO_PATH));
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
//		tempButton = (Button) findViewById(R.id.tempbutton);
//		tempButton.setOnClickListener(this);

		next_main_btn = (Button) findViewById(R.id.next_main_btn);
		play_main_btn = (Button) findViewById(R.id.play_main_btn);
		prevail_main_btn = (Button) findViewById(R.id.prevail_main_btn);
		next_main_btn.setOnClickListener(this);
		play_main_btn.setOnClickListener(this);
		prevail_main_btn.setOnClickListener(this);
	}

	private void addMusicList() {

		// init adapters
		mianplayer_music_adapter = new MusicListAdapter(ctx, music_data);
		mianplayer_music_viewlist.setAdapter(mianplayer_music_adapter);

	}

	private void temp_getdata() {
		music_data = FileCommonUtil.findMP3InSD(ctx);
	}

	public void onClick(View v) {
		int id = v.getId();
		try {
			switch (id) {
			case R.id.next_main_btn:
				musicControlServer.next();
				break;
			case R.id.prevail_main_btn:
				musicControlServer.prevail();
				break;
			case R.id.play_main_btn:
				musicControlServer.start();
				break;
//			case R.id.tempbutton:
//				temp_getdata();
//				addMusicList();
//				break;

			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}

	private void initMusicServer() {
		controlServerConnect = new MusicControlServerConnect();
		Intent musicServer = new Intent();
		musicServer.setAction(this.getString(R.string.music_server_action));
		bindService(musicServer, controlServerConnect,
				Context.BIND_AUTO_CREATE);
	}

	class MusicControlServerConnect implements ServiceConnection {
		public void onServiceConnected(ComponentName name, IBinder service) {
			musicControlServer = IMusicControlServer.Stub.asInterface(service);
		}

		public void onServiceDisconnected(ComponentName name) {
			musicControlServer = null;
		}

	}
	

}